package com.project.controller;

import com.project.bean.BuyerCarBean;
import com.project.bean.ProductBean;
import com.project.bean.UserBean;
import com.project.service.IBuyerCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("buyerCar")
public class BuyerCarController {
    @Autowired
    private IBuyerCarService buyerCarService;

    @RequestMapping("findByUserId")
    public List<BuyerCarBean> findByUserId(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
//        System.out.println(buyerCarService.findByUserId(user.getId()));
        return buyerCarService.findByUserId(user.getId());
//        return null;
    }

    @RequestMapping("add")
    public String add(String option, ProductBean product, HttpServletRequest req, Integer num) {
        System.out.println(product);
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        buyerCarService.add(option, product, user.getId(), num);
        return "ok";
//        return null;
    }

    @RequestMapping("del")
    public String remove(Integer buyCarId) {
        buyerCarService.del(buyCarId);
        return "ok";
    }

    @RequestMapping("update")
    public String updateNum(Integer buyCarId, Integer num) {
        buyerCarService.update(buyCarId, num);
        return "ok";
    }
}
